package com.lyf.core.service.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lyf.core.dao.UserDao;
import com.lyf.core.pojo.User;
import com.lyf.core.pojo.model.PageModel;
import com.lyf.core.service.UserService;
import com.lyf.core.util.CommonUtils;

@Service
public class UserServiceImpl implements UserService{
	
		@Autowired
		private UserDao userDao;
		
		public String save(User u) {
			
			if(u == null || u.getUser_id() == null){
				return "用户为空" ;
			}
			if(u.getUser_pass() == null){
				return "用户密码为空" ;
			}
			
			//检查id是否被占用
			User _u = findByUserId(u.getUser_id());
			if(_u != null){
				return "该用户ID已存在" ;
			}
			if(u.getCreate_time() != null){
				u.setCreate_time(new Date());
			}
			
			userDao.insert(u);
			
			return null ;
		}
		
		@Override
		public User findById(int uid) {
		// TODO Auto-generated method stub
			return userDao.findById(uid);
		}
		
		public User findByUserId(String user_id) {
			
			if(user_id.equals(SUPER_ADMIN_ID)) {
				User u = new User();
				u.setId(0);
				u.setUser_id(SUPER_ADMIN_ID);
				u.setUser_pass(SUPER_ADMIN_PASS);
				u.setUser_name("超管");
				return u ;
			}
			 
			return userDao.findByUserId(user_id) ;
		}
		
	
		 
		 
		
		/**
		 * 更新用户密码
		 * @param user_id
		 * @param newpass
		 * @return
		 */
		public boolean updateUserPass(int uid ,String oldpass , String newpass) {
			
			User u = findById(uid);
			if(u == null) {
				throw new RuntimeException("用户不存!");
			}
			
			//old pass md5 加密
			String oldMd5 = CommonUtils.md5(u.getUser_id(), oldpass);
			
			if(!u.getUser_pass().equals(oldMd5)) {
				throw new RuntimeException("密码不对!");
			}
			
			String md5 = CommonUtils.md5(u.getUser_id(), newpass);
			
			u.setUser_pass(md5);
			
			userDao.updateUser(u);
			
			
			return true;
		}
		
		/**
		 * 用户重置密码
		 * @param user_id
		 * @param pass
		 */
		public boolean restPass(int uid , String pass) {
			User u = findById(uid);
			if(u == null) {
				throw new RuntimeException("用户不存在!");
			}
			
			String md5 = CommonUtils.md5(u.getUser_id(), pass);
			 
			
			userDao.resetPass(uid, md5);
			
			return true ;
		}
		
		 
		
		/**
		 * 查找用户信息
		 * @param key 关键字
		 * @param rows 每页数量
		 * @param page 页
		 * @return
		 */
		public PageModel<User> findUsers(Integer dept_id , String key , int rows , int page) {
			 
			int size = userDao.findUserSize(dept_id , key) ;
			
			List<User> list = size > 0 ? userDao.findUserList(dept_id , key, rows, page) : new LinkedList<User>();
			
			PageModel<User> model = new PageModel<>();
			model.setPage(page);
			model.setTotal(size);
			model.setRow(rows);
			model.setRows(list);

			return model ;
			
		}
		
		   
		@Override
		public void deleteUser(int uid) {
			// TODO Auto-generated method stub
			userDao.delete(uid);
			
			
		}

		@Override
		public String update(User user) {
			
			userDao.updateUser(user);
			
			//更新用户角色
			  
			return null;
		}

	 
}
